package cn.xshi.monitor.web;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import cn.xshi.monitor.service.IotModelService;
import io.swagger.annotations.Api;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.monitor.model.IotModel;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import cn.xshi.common.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BaseSearch;

import javax.annotation.Resource;
/**
 * @Desc 设备型号
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/iotModel")
@Api(value = "设备型号",tags = "设备型号", description = "设备型号")
public class IotModelController extends BaseAction{

	@Resource
	private IotModelService iotModelService;

	/**
	* 查询并分页
	* @param baseSearch 
	*/
	@ApiOperation(value="查询并分页", notes="查询并分页")
	@PostMapping(value="/list")
	@Auth(value = "/iotModel/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<IotModel>> getIotModelListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<IotModel> iotModelList = iotModelService.getIotModelListByCondition(condition);
		for(IotModel iotModel:iotModelList){
			if(!StringUtil.isEmpty(iotModel.getCreateId())){
				OauthAccountEntity createBy = getAccount(iotModel.getCreateId());
				if(null != createBy){
					iotModel.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(iotModel.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(iotModel.getUpdateId());
				if(null != modifiedBy){
					iotModel.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<IotModel> page = new PageInfo<IotModel>(iotModelList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单条记录
	* @param id
	*/
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/iotModel/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<IotModel> getIotModelById(@PathVariable("id")String id){
		IotModel iotModel = iotModelService.getIotModelById(id);
		if(!StringUtil.isEmpty(iotModel.getCreateId())){
			OauthAccountEntity createBy = getAccount(iotModel.getCreateId());
			if(null != createBy){
				iotModel.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(iotModel.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(iotModel.getUpdateId());
			if(null != modifiedBy){
				iotModel.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(iotModel);
	}

	/**
	* 添加
	* @param iotModel 
	*/
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value="/add")
	public BaseResult addIotModel(@RequestBody IotModel iotModel){
		int i = 0;
		if(null != iotModel){
			i=iotModelService.addIotModel(iotModel);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param iotModel 
	*/
	@ApiOperation(value="修改", notes="修改")
	@PutMapping(value="/update")
	public BaseResult updateIotModel(@RequestBody IotModel iotModel){
		int i = 0;
		if(null != iotModel){
			i=iotModelService.updateIotModel(iotModel);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除", notes="删除")
	@DeleteMapping(value="/delete")
	public BaseResult delIotModel(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=iotModelService.delIotModel(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 根据生产商查找
	 * @param oemId
	 */
	@ApiOperation(value="根据生产商查找", notes="根据生产商查找")
	@GetMapping(value="/list/{oemId}")
	@Auth(value = "/iotModel/list/oemId",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<IotModel>> listByOemId(@PathVariable("oemId")String oemId){
		Map<String,Object> condition = new HashMap<>();
		condition.put("oemId",oemId);
		List<IotModel> iotModelList = iotModelService.getIotModelListByCondition(condition);
		return new BaseResult(iotModelList);
	}
}
